package com.comunity_cloud.client.view2;

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.util.Collection;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import com.comunity_cloud.client.controller.MainController2;
import com.comunity_cloud.client.model.MainModel2;
import com.user_hosted_storage.dto.CommunityDTO;

public class MainView extends JFrame {

	private MainController2 controller;
	
	private JTabbedPane jTabbedPane;
	
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public MainView(final MainController2 controller) {
		
		this.controller = controller;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		jTabbedPane = new JTabbedPane();
		jTabbedPane.setTabPlacement(JTabbedPane.LEFT);
		jTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
//		setLayout(new BorderLayout());
		
		add(jTabbedPane);
		setSize(400, 400);
		
		try {
			TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/images/share.png")).getImage());
			trayIcon.setToolTip("Community storage - "+MainModel2.getInstance().getClientDTO().getUserName());
			SystemTray.getSystemTray().add(trayIcon );
		} catch (HeadlessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	public void initCommunities() {
		setTitle(controller.getClient().getUserName());
		
		Collection<CommunityDTO> communities = controller.getMyCommunities();
		int count = 0;
		for (final CommunityDTO communityDTO : communities) {
			final CommunityPanel filesPanel = new CommunityPanel(communityDTO);
			
			jTabbedPane.add(communityDTO.getName(), filesPanel);
			jTabbedPane.setTabComponentAt(count++, new ButtonTabComponent(jTabbedPane,communityDTO));
		}
	}
	
}
